package com.tiger.communal.service.impl;

import java.util.List;

import com.tiger.common.utils.DateUtils;
import com.tiger.common.utils.StringUtils;
import com.tiger.communal.domain.CommonAgency;
import com.tiger.communal.domain.CommonAgencyEnclosure;
import com.tiger.communal.domain.CommonAgencyLinkman;
import com.tiger.communal.mapper.CommonAgencyEnclosureMapper;
import com.tiger.communal.mapper.CommonAgencyLinkmanMapper;
import com.tiger.communal.mapper.CommonAgencyMapper;
import com.tiger.communal.service.ICommonAgencyService;
import com.tiger.project.domain.Asset;
import com.tiger.project.domain.ContractSponsor;
import com.tiger.project.mapper.AssetMapper;
import com.tiger.project.mapper.ContractSponsorMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 代营公司Service业务层处理
 *
 * @author tiger
 * @date 2022-09-13
 */
@Service
public class CommonAgencyServiceImpl implements ICommonAgencyService
{
    @Autowired
    private CommonAgencyMapper commonAgencyMapper;
    @Autowired
    private CommonAgencyLinkmanMapper linkmanMapper;
    @Autowired
    private CommonAgencyEnclosureMapper enclosureMapper;
    @Autowired
    private AssetMapper assetMapper;
    @Autowired
    private ContractSponsorMapper sponsorMapper;

    /**
     * 查询代营公司
     *
     * @param id 代营公司主键
     * @return 代营公司
     */
    @Override
    public CommonAgency selectCommonAgencyById(Long id) {
        CommonAgency commonAgency = commonAgencyMapper.selectCommonAgencyById(id);

        //联系人
        CommonAgencyLinkman linkman = new CommonAgencyLinkman();
        linkman.setAgencyId(id);
        List<CommonAgencyLinkman> commonAgencyLinkmen = linkmanMapper.selectCommonAgencyLinkmanList(linkman);
        if (commonAgencyLinkmen != null && commonAgencyLinkmen.size()>0){
            commonAgency.getLinkmanList().addAll(commonAgencyLinkmen);
        }

        //附件
        CommonAgencyEnclosure enclosure = new CommonAgencyEnclosure();
        enclosure.setAgencyId(id);
        List<CommonAgencyEnclosure> commonAgencyEnclosures = enclosureMapper.selectCommonAgencyEnclosureList(enclosure);
        if (commonAgencyEnclosures != null && commonAgencyEnclosures.size() > 0){
            commonAgency.getEnclosureList().addAll(commonAgencyEnclosures);
        }

        //资产
        Asset asset = new Asset();
        asset.setPropertyOwnerId(id);
        List<Asset> assets = assetMapper.selectAssetList(asset);
        if (assets != null && assets.size() > 0){
            commonAgency.getAssetList().addAll(assets);
        }

        //合同发起
        ContractSponsor sponsor = new ContractSponsor();
        sponsor.setLessorId(id);
        List<ContractSponsor> sponsorList = sponsorMapper.selectContractSponsorList(sponsor);
        if (sponsorList != null && sponsorList.size() > 0){
            commonAgency.getSponsorList().addAll(sponsorList);
        }

        return commonAgency;
    }

    /**
     * 查询代营公司列表
     *
     * @param commonAgency 代营公司
     * @return 代营公司
     */
    @Override
    public List<CommonAgency> selectCommonAgencyList(CommonAgency commonAgency) {
        return commonAgencyMapper.selectCommonAgencyList(commonAgency);
    }

    /**
     * 新增代营公司
     *
     * @param commonAgency 代营公司
     * @return 结果
     */
    @Override
    public int insertCommonAgency(CommonAgency commonAgency) {
        commonAgency.setCreatedAt(DateUtils.getNowDate());
        commonAgency.setIsDeleted(0L);
        int i = commonAgencyMapper.insertCommonAgency(commonAgency);
        change(commonAgency);
        return i;
    }

    /**
     * 修改代营公司
     *
     * @param commonAgency 代营公司
     * @return 结果
     */
    @Override
    public int updateCommonAgency(CommonAgency commonAgency) {
        change(commonAgency);
        commonAgency.setUpdateAt(DateUtils.getNowDate());
        return commonAgencyMapper.updateCommonAgency(commonAgency);
    }

    /**
     * 批量删除代营公司
     *
     * @param ids 需要删除的代营公司主键
     * @return 结果
     */
    @Override
    public int deleteCommonAgencyByIds(Long[] ids) {
        return commonAgencyMapper.deleteCommonAgencyByIds(ids);
    }

    /**
     * 删除代营公司信息
     *
     * @param id 代营公司主键
     * @return 结果
     */
    @Override
    public int deleteCommonAgencyById(Long id) {
        return commonAgencyMapper.deleteCommonAgencyById(id);
    }

    private void change(CommonAgency commonAgency){
        //联系人
        linkmanMapper.deleteCommonAgencyLinkmanByCommonAgencyId(commonAgency.getId());
        if (StringUtils.isNotEmpty(commonAgency.getLinkmanList())){
            commonAgency.getLinkmanList().forEach(item -> {
                item.setAgencyId(commonAgency.getId());
                linkmanMapper.insertCommonAgencyLinkman(item);
            });
        }

        // 附件
        enclosureMapper.deleteCommonAgencyEnclosureByCommonAgency(commonAgency.getId());
        if (StringUtils.isNotEmpty(commonAgency.getEnclosureList())){
            commonAgency.getEnclosureList().forEach(enclosureItem ->{
                enclosureItem.setAgencyId(commonAgency.getId());
                enclosureItem.setCreatedAt(DateUtils.getNowDate());
                enclosureMapper.insertCommonAgencyEnclosure(enclosureItem);
            });
        }
    }



}
